package org.itboys.core.service.mysql.front;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.ExceptionEnum;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.coupon.SundayCouponMapper;
import org.itboys.core.dao.coupon.SundayCouponRecordMapper;
import org.itboys.core.dto.coupon.SundayCouponDTO;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mysql.coupon.SundayCoupon;
import org.itboys.core.entity.mysql.coupon.SundayCouponExample;
import org.itboys.core.entity.mysql.coupon.record.SundayMemberCoupon;
import org.itboys.core.entity.mysql.coupon.record.SundayMemberCouponExample;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.util.MybatisParamUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
*@Author: jiaqilei
*@Date: 11:41 2018/12/13
*@Discription: 移动端优惠券
*/
@Service
public class SundayFrontCouponService{

    @Autowired
    private SundayCouponMapper couponMapper;
    @Autowired
    private SundayCouponRecordMapper couponRecordMapper;
    @Autowired
    private SundayMemberService memberService;

    /**
     * 查询优惠券列表，分页，如果memberId为空查询所有优惠券
     * @param paramUtil
     * @param memberId
     * @return
     */
    public List<SundayCouponDTO> select(MybatisParamUtil paramUtil,Long memberId) {
        List<SundayCouponDTO> result=new ArrayList<>();
        if(!LongUtil.isNotZreo(memberId)){
            SundayCouponExample example=new SundayCouponExample();
            example.setStartRow(paramUtil.getRowStart());
            example.setSortOrder(paramUtil.getSortOrder());
            example.setPageSize(paramUtil.getRowSize());
            example.setOrderByClause(paramUtil.getSortName());
            example.createCriteria().andIsDeletedEqualTo(SundayCommonConstants.NO);
            List<SundayCoupon> coupons = couponMapper.selectByExample(example);
            result=packageDTOs(coupons);
        }else {
            SundayMemberCouponExample example=new SundayMemberCouponExample();
            example.setStartRow(paramUtil.getRowStart());
            example.setSortOrder(paramUtil.getSortOrder());
            example.setPageSize(paramUtil.getRowSize());
            example.setOrderByClause(paramUtil.getSortName());
            example.createCriteria().andIsDeletedEqualTo(SundayCommonConstants.NO)
                    .andMemberIdEqualTo(memberId);
            List<SundayMemberCoupon> memberCoupons = couponRecordMapper.selectByExample(example);
            if(ListUtil.isNotNull(memberCoupons)){
                List<Long> ids=new ArrayList<>();
                for (SundayMemberCoupon mc:memberCoupons
                     ) {
                    ids.add(mc.getCouponId());
                }
                SundayCouponExample example1=new SundayCouponExample();
                example1.setStartRow(paramUtil.getRowStart());
                example1.setSortOrder(paramUtil.getSortOrder());
                example1.setPageSize(paramUtil.getRowSize());
                example1.setOrderByClause(paramUtil.getSortName());
                example1.createCriteria().andIsDeletedEqualTo(SundayCommonConstants.NO);
                List<SundayCoupon> coupons = couponMapper.selectByExample(example1);
                result=packageDTOs(coupons);
            }
        }
        return result;
    }

    public List<SundayCouponDTO> packageDTOs(List<SundayCoupon> coupons){
        List<SundayCouponDTO> result=new ArrayList<>();
        if(ListUtil.isNotNull(coupons)){
            for (SundayCoupon c:coupons
                    ) {
                SundayCouponDTO dto=new SundayCouponDTO();
                BeanUtils.copyProperties(c,dto);
                result.add(dto);
            }
        }
        return result;
    }

    public void receive(Long memberId,Long couponId){
        //Long memberId, Long couponId, Integer type, Integer used, BigDecimal money, String memberName, String mobile
        SundayMember member = memberService.getById(memberId);
        if(member==null) throw new ErrorException(ExceptionEnum.USER_NONENTITY.getValue());
        SundayCoupon coupon = couponMapper.select(couponId);
        SundayMemberCoupon memberCoupon=new SundayMemberCoupon(memberId,couponId,SundayCommonConstants.COUPON_TYPE_GRANT
                ,SundayCommonConstants.NO,coupon.getMoney(),member.getRealName(),member.getMobile());
        couponRecordMapper.insert(memberCoupon);
    }
    /**
     * 根据用户id返回一个未使用的优惠券
     * @param memberId
     * @return
     */
    public SundayCoupon getByMemberId(Long memberId){
        SundayMemberCouponExample example=new SundayMemberCouponExample();
        example.createCriteria().andIsDeletedEqualTo(SundayCommonConstants.NO)
                .andMemberIdEqualTo(memberId).andUsedEqualTo(SundayCommonConstants.NO);
        List<SundayMemberCoupon> memberCoupons = couponRecordMapper.selectByExample(example);
        if(ListUtil.isNull(memberCoupons))return null;
        List<Long> ids=new ArrayList<>();
        for (SundayMemberCoupon mc:memberCoupons
                ) {
            ids.add(mc.getCouponId());
        }
        SundayCouponExample _e=new SundayCouponExample();
        _e.createCriteria().andIsDeletedEqualTo(SundayCommonConstants.NO).andIdIn(ids);
        List<SundayCoupon> coupons = couponMapper.selectByExample(_e);
        if(ListUtil.isNull(coupons))return null;
        return coupons.get(0);
    }

    /**
     * 使用一张优惠券
     * @param memberId 用户id
     * @param couponId 优惠券id
     */
    public void updateState(Long memberId,Long couponId){
        SundayMemberCouponExample example=new SundayMemberCouponExample();
        //查找出用户的领用优惠券,状态为未使用的
        example.createCriteria().andMemberIdEqualTo(memberId).andCouponIdEqualTo(couponId)
                .andIsDeletedEqualTo(SundayCommonConstants.NO).andUsedEqualTo(SundayCommonConstants.NO);
        List<SundayMemberCoupon> memberCoupons = couponRecordMapper.selectByExample(example);
        if(ListUtil.isNull(memberCoupons))throw new ErrorException(ExceptionEnum.COUPON_USED_ERROR.getValue());
        //选择其中的一张状态改为已使用
        SundayMemberCoupon memberCoupon=memberCoupons.get(0);
        memberCoupon.setUsed(SundayCommonConstants.YES) ;
        couponRecordMapper.update(memberCoupon);
    }
}
